/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddCustomer.java
 *
 * Created on Dec 18, 2011, 11:09:59 AM
 */
package view.manage.customers;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JOptionPane;
import main.WinkelApplication;
import model.Klant;

/**
 *
 * @author loki
 */
public class AddCustomer extends javax.swing.JPanel {

    /** Creates new form AddCustomer */
    public AddCustomer() {
        initComponents();
    }
    
    //Copied from: AddUser.java
    String getDateAndTime() {
        final String date = "yyyy-MM-dd HH:mm:ss";
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat(date);
        return dateFormat.format(calendar.getTime());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextUser = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextEmail = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jPass = new javax.swing.JPasswordField();
        jPassRepeat = new javax.swing.JPasswordField();
        jButtonBack = new javax.swing.JButton();
        jButtonNext = new javax.swing.JButton();
        jButtonAdd = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jTextFirstName = new javax.swing.JTextField();
        jTextLastName = new javax.swing.JTextField();
        jTextHome = new javax.swing.JTextField();
        jTextAdres = new javax.swing.JTextField();
        jTextPostalCode = new javax.swing.JTextField();
        jLabelShowError = new javax.swing.JLabel();

        jLabel1.setText("Gebruikersnaam:");

        jLabel2.setText("Wachtwoord:");

        jLabel3.setText("Herhaal wachtwoord:");

        jLabel4.setText("E-mail:");

        jButtonBack.setText("Vorige");
        jButtonBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBackActionPerformed(evt);
            }
        });

        jButtonNext.setText("Volgende");
        jButtonNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNextActionPerformed(evt);
            }
        });

        jButtonAdd.setText("Toevoegen");
        jButtonAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddActionPerformed(evt);
            }
        });

        jLabel5.setText("Gebruikers gegevens:");

        jLabel6.setText("Klanten gegevens:");

        jLabel7.setText("Voornaam:");

        jLabel8.setText("Achternaam:");

        jLabel9.setText("Woonplaats:");

        jLabel10.setText("Postcode:");

        jLabel11.setText("Adres:");

        jLabelShowError.setForeground(java.awt.Color.red);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(650, 650, 650)
                .addComponent(jButtonAdd)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel1))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(108, 108, 108)
                                .addComponent(jLabel4)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextEmail)
                            .addComponent(jPassRepeat)
                            .addComponent(jPass)
                            .addComponent(jTextUser, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 114, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel10)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8)
                            .addComponent(jLabel9)
                            .addComponent(jLabel11))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextFirstName, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE)
                            .addComponent(jTextLastName)
                            .addComponent(jTextHome)
                            .addComponent(jTextAdres)
                            .addComponent(jTextPostalCode))
                        .addGap(97, 97, 97))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonBack, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonNext, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(581, Short.MAX_VALUE))))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 255, Short.MAX_VALUE)
                .addComponent(jLabel6)
                .addGap(191, 191, 191))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelShowError)
                .addContainerGap(740, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonBack)
                    .addComponent(jButtonNext))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 47, Short.MAX_VALUE)
                .addComponent(jLabelShowError)
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5))
                .addGap(45, 45, 45)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(40, 40, 40)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jTextLastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel8))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jTextHome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel9))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jTextAdres, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel11)))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jTextFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel7)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextPostalCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel10))
                        .addGap(138, 138, 138))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jPassRepeat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addGap(128, 128, 128)
                        .addComponent(jButtonAdd))))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBackActionPerformed
        // TODO add your handling code here:
        WinkelApplication.window.previous();
    }//GEN-LAST:event_jButtonBackActionPerformed

    private void jButtonNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNextActionPerformed
        // TODO add your handling code here:
        WinkelApplication.window.next();
    }//GEN-LAST:event_jButtonNextActionPerformed

    private void jButtonAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddActionPerformed
        // TODO add your handling code here:
        
        //First check all forms for illegal characters.
        if(!cleanSQL(jTextEmail.getText())) {
            jLabelShowError.setText("Textveld E-mail bevat illegale text.");
            return;
        }
        if(!cleanSQL(jTextUser.getText())) {
            jLabelShowError.setText("Textveld Gebruikersnaam bevat illegale text.");
            return;
        }
        if(!cleanSQL(jTextFirstName.getText())) {
            jLabelShowError.setText("Textveld Voornaam bevat illegale text.");
            return;
        }
        if(!cleanSQL(jTextLastName.getText())) {
            jLabelShowError.setText("Textveld Achternaam bevat illegale text.");
            return;
        }
        if(!cleanSQL(jTextHome.getText())) {
            jLabelShowError.setText("Textveld Woonplaats bevat illegale text.");
            return;
        }
        if(!cleanSQL(jTextPostalCode.getText())) {
            jLabelShowError.setText("Textveld Postcode bevat illegale text.");
            return;
        }
        if(!cleanSQL(jTextAdres.getText())) {
            jLabelShowError.setText("Textveld Woonplaats bevat illegale text.");
            return;
        }
        
        if(passCorrect()) {
            jLabelShowError.setVisible(false);
            
            String password = "";
            //Convert from char[] to String
            
            JOptionPane.showMessageDialog(WinkelApplication.getInstance().getMainWindow(), "Klant" + 
                    jTextLastName.getText() + ", " +jTextFirstName.getText() + " toegevoegd.");
            for(char letter : jPass.getPassword()) {
                password += letter;
            }
            Klant klant = new Klant();
            
            klant.setAdres(jTextAdres.getText());
            klant.setNaam(jTextLastName.getText() + ", " +jTextFirstName.getText());
            klant.setPostcode(jTextPostalCode.getText());
            klant.setWoonplaats(jTextHome.getText());
            klant.setRol_id(1);
            klant.setGebruikersnaam(jTextUser.getText());
            klant.setEmail(jTextEmail.getText());
            klant.setWachtwoord(password);
            klant.setAangemaakt_op(getDateAndTime());
            klant.setLaatst_ingelogd(getDateAndTime());
            //Put customer inside the database.
            WinkelApplication.getQueryManager().addKlant(klant);
        }
    }//GEN-LAST:event_jButtonAddActionPerformed
    //Test wether or not the textfields contain illegal characters.
    private boolean cleanSQL(String test) {
        String allowed = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@.-_+ ";
        int length_x = test.length();
        for(int x = 0; x < length_x; x++) {
            //The function match doesnt accept chars.
            String pattern = ""+test.charAt(x);
            
            if(allowed.indexOf(pattern) == -1) {
                return false;
            }
        }
        return true;
    }
    private boolean passCorrect() {
        char[] pass = jPass.getPassword();
        char[] passRepeat = jPassRepeat.getPassword();
        
        if(pass.length < 5) {
            jLabelShowError.setText("Het wachtwoord moet minimaal 5 characters bevatten.");
            return false;
        }
        if(pass.length > 20) {
            jLabelShowError.setText("Het wachtwoord mag maximaal 20 characters bevatten.");
            return false;
        }
        if(pass.length == passRepeat.length) {
            int length = pass.length;
            for(int x = 0; x < length; x++) {
                if(pass[x] != passRepeat[x]) {
                    //Passwords are not the same.
                    jLabelShowError.setText("Het wachtwoord in beide textvelden zijn niet hetzelfde.");
                    jLabelShowError.setVisible(true);
                    return false;
                }
            }
        }
        else {
            //Password lengths aren't even the same.
            jLabelShowError.setText("Het wachtwoord in beide textvelden zijn niet hetzelfde.");
            jLabelShowError.setVisible(true);
            return false;
        }
        String test = "";
        for(int x = 0; x < pass.length; x++) {
            test += pass[x];
        }
        if(!cleanSQL(test)) {
            jLabelShowError.setText("Het wachtwoord bevat illegale text.");
            return false;
        }
        return true;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAdd;
    private javax.swing.JButton jButtonBack;
    private javax.swing.JButton jButtonNext;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel jLabelShowError;
    private javax.swing.JPasswordField jPass;
    private javax.swing.JPasswordField jPassRepeat;
    private javax.swing.JTextField jTextAdres;
    private javax.swing.JTextField jTextEmail;
    private javax.swing.JTextField jTextFirstName;
    private javax.swing.JTextField jTextHome;
    private javax.swing.JTextField jTextLastName;
    private javax.swing.JTextField jTextPostalCode;
    private javax.swing.JTextField jTextUser;
    // End of variables declaration//GEN-END:variables
}
